package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.institution.Institution;
import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.vo.PageBean;

public class MedicalDao extends GenericDao<Medical>{
	@Override
	protected Medical handle(ResultSet rs) throws SQLException {
		
		Medical entity =new Medical();
		entity.setJgbm(rs.getString("jgbm"));
		entity.setZzjgbm(rs.getString("zzjgbm"));
		entity.setJgmc(rs.getString("jgmc"));
		entity.setDqbm(rs.getString("dqbm"));
		entity.setAreacode(rs.getString("areacode"));
		entity.setLsgx(rs.getString("lsgx"));
		entity.setJgjb(rs.getString("jgjb"));
		entity.setSbddlx(rs.getString("sbddlx"));
		entity.setPzddlx(rs.getString("pzddlx"));
		entity.setSsjjlx(rs.getString("ssjjlx"));
		entity.setWsjgdl(rs.getString("wsjgdl"));
		entity.setWsjgxl(rs.getString("wsjgxl"));
		entity.setZgdw(rs.getString("zgdw"));
		entity.setKysj(rs.getDate("kysj"));
		entity.setFrdb(rs.getString("frdb"));
		entity.setZczj(rs.getDouble("zczj"));
		return entity;

	}
	
	public void add(Medical m)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			DbUtil db = new DbUtil();
			conn=db.getConn();
			String sql = "insert into t_medical(jgbm, zzjgbm, jgmc, dqbm, areacode, lsgx, jgjb, sbddlx, pzddlx, ssjjlx, wsjgdl, wsjgxl, zgdw, kysj, frdb,zczj) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++, m.getJgbm());
			pstmt.setString(index++, m.getZzjgbm());
			pstmt.setString(index++, m.getJgmc());
			pstmt.setString(index++, m.getDqbm());
			pstmt.setString(index++, m.getAreacode());
			pstmt.setString(index++, m.getLsgx());
			pstmt.setString(index++, m.getJgjb());
			pstmt.setString(index++, m.getSbddlx());
			pstmt.setString(index++, m.getPzddlx());
			pstmt.setString(index++, m.getSsjjlx());
			pstmt.setString(index++, m.getWsjgdl());
			pstmt.setString(index++, m.getWsjgxl());
			pstmt.setString(index++, m.getZgdw());
			pstmt.setDate(index++, new java.sql.Date(m.getKysj().getTime()));
			pstmt.setString(index++, m.getFrdb());
			pstmt.setDouble(index++, m.getZczj());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("添加医疗机构失败 " + e.getMessage(), e);
		}finally{
			pstmt.close();
			conn.close();
		}
	}
	public void del(String jgbm)throws SQLException{
		try {
			DbUtil db = new DbUtil();
			String sql = "delete from t_medical where jgbm='" + jgbm + "'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除慢性病失败" + e.getMessage(), e);
		}
	}
	public Medical query(String jgbm)throws SQLException{
		String sql ="select * from t_medical where jgbm=?";
		Object[] params = {jgbm};
		List<Medical> list = new ArrayList<Medical>();
		list = super.queryAll(sql, params);
		if(list.size()!=0)
			return list.get(0);
		else
			return null;
	}	
	public List<Medical> queryAll()throws SQLException{
		String sql ="select * from t_medical";
		return super.queryAll(sql, null);
	}
	public PageBean queryAll(int page,int size)throws SQLException{
		String sql ="select * from t_medical";
		return super.queryAll(sql, null, page, size);
	}
	public void update(Medical m)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			DbUtil db = new DbUtil();
			conn=db.getConn();
			String sql = "update t_medical set zzjgbm=?, jgmc=?, dqbm=?, areacode=?, lsgx=?, jgjb=?, sbddlx=?, pzddlx=?, ssjjlx=?, wsjgdl=?, wsjgxl=?, zgdw=?, kysj=?, frdb=?,zczj=? where jgbm=?";
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++, m.getZzjgbm());
			pstmt.setString(index++, m.getJgmc());
			pstmt.setString(index++, m.getDqbm());
			pstmt.setString(index++, m.getAreacode());
			pstmt.setString(index++, m.getLsgx());
			pstmt.setString(index++, m.getJgjb());
			pstmt.setString(index++, m.getSbddlx());
			pstmt.setString(index++, m.getPzddlx());
			pstmt.setString(index++, m.getSsjjlx());
			pstmt.setString(index++, m.getWsjgdl());
			pstmt.setString(index++, m.getWsjgxl());
			pstmt.setString(index++, m.getZgdw());
			pstmt.setDate(index++, new java.sql.Date(m.getKysj().getTime()));
			pstmt.setString(index++, m.getFrdb());
			pstmt.setDouble(index++, m.getZczj());
			pstmt.setString(index++, m.getJgbm());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("修改医疗机构失败 " + e.getMessage(), e);
		}
		finally{
			pstmt.close();
			conn.close();
		}
	}
	
	
	public static void main(String[]args) throws SQLException{
		MedicalDao m = new MedicalDao();
		//m.del("1");
		
		/*Medical entity =new Medical();
		entity.setJgbm("1");
		entity.setZzjgbm("1");
		entity.setJgmc("1");
		entity.setDqbm("1");
		entity.setAreacode("1");
		entity.setLsgx("1");
		entity.setJgjb("1");
		entity.setSbddlx("1");
		entity.setPzddlx("1");
		entity.setSsjjlx("1");
		entity.setWsjgdl("1");
		entity.setWsjgxl("1");
		entity.setKysj(new Date());
		entity.setZgdw("1");
		entity.setFrdb("1");
		entity.setZczj(200.00);
		m.add(entity);*/
	}
}
